package com.brillilab.service.core.space.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.common.utils.MyBeanUtils;
import com.brillilab.dao.mapper.inventory.ReagentInventoryMapper;
import com.brillilab.dao.mapper.kit.KitMapper;
import com.brillilab.dao.mapper.space.KitBoxMapper;
import com.brillilab.dao.mapper.space.PanelGridUsedMapper;
import com.brillilab.domain.enums.common.IsDeleteEnum;
import com.brillilab.domain.enums.lab.LabMemberPowerEnum;
import com.brillilab.domain.enums.space.SpaceTypeEnum;
import com.brillilab.domain.po.inventory.ReagentInventory;
import com.brillilab.domain.po.kit.Kit;
import com.brillilab.domain.po.space.KitBox;
import com.brillilab.domain.po.space.PanelGridUsed;
import com.brillilab.domain.vo.space.KitBoxVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.space.IKitBoxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.brillilab.domain.enums.space.SpaceTypeEnum.*;

/**
 * @author wuzhiyu
 * @Title: KitBoxServiceImpl
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2018/11/159:09
 */
@Service
@Transactional
public class KitBoxServiceImpl implements IKitBoxService {
    @Autowired
    private KitBoxMapper kitBoxMapper;
    @Autowired
    private PanelGridUsedMapper panelGridUsedMapper;
    @Autowired
    private ReagentInventoryMapper reagentInventoryMapper;
    @Autowired
    private KitMapper kitMapper;

    @Override
    public KitBox get(Long id) {
        return kitBoxMapper.selectById(id);
    }

    @Override
    public KitBox add(KitBox kitBox) {
        kitBoxMapper.insert(kitBox);
        return kitBox;
    }

    @Override
    public Integer update(KitBox kitBox) {
        return kitBoxMapper.updateById(kitBox);
    }

    @Override
    public List<KitBoxVo> list(Long panelId, UserInfoVo user) {
        LambdaQueryWrapper<KitBox> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KitBox::getPanelId, panelId);
        wrapper.eq(KitBox::getIsDelete, IsDeleteEnum.NO.getValue());
        LambdaQueryWrapper<PanelGridUsed> wrapperGrid = new LambdaQueryWrapper<>();
        wrapperGrid.eq(PanelGridUsed::getPanelId, panelId);
        wrapperGrid.isNotNull(PanelGridUsed::getKitBoxId);
        List<PanelGridUsed> gridUsedList = panelGridUsedMapper.selectList(wrapperGrid);
        List<KitBox> boxList = kitBoxMapper.selectList(wrapper);

        LambdaQueryWrapper<ReagentInventory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReagentInventory::getPanelId,panelId);
        queryWrapper.eq(ReagentInventory::getIsDelete,IsDeleteEnum.NO.getValue());
        queryWrapper.isNotNull(ReagentInventory::getKitBoxId);
        List<ReagentInventory> reagentInventoryList=reagentInventoryMapper.selectList(queryWrapper);
        List<Kit> kitList=kitMapper.selectNameByPanelIdAndNotDelete(panelId);

        List<KitBoxVo> boxVoList = new ArrayList<>();
        boxList.forEach(b -> {
            KitBoxVo bv = new KitBoxVo();
            MyBeanUtils.copyProperties(b, bv);
            Optional<ReagentInventory> inventoryOpt=reagentInventoryList.stream().filter(r -> r.getKitBoxId().equals(b.getId())).findFirst();
            if(inventoryOpt.isPresent()){
                Optional<Kit> kitOpt=kitList.stream().filter(k -> k.getId().equals(inventoryOpt.get().getKitId())).findFirst();
                if(kitOpt.isPresent()){
                    bv.setKitName(kitOpt.get().getName());
                }
            }

            List<PanelGridUsed> usedList = gridUsedList.stream().filter(g -> g.getKitBoxId().equals(b.getId())).collect(Collectors.toList());
            bv.setPanelGridUsedList(usedList);
            bv.setCanSee(bv.getLabMemberId() == null ? true:(user.hasPower(LabMemberPowerEnum.Power.REAGENT)||b.getLabMemberId().equals(user.getLabMemberId())));
            boxVoList.add(bv);
        });
        return boxVoList;
    }

    @Override
    public boolean updateRoomName(Long roomId, String name) {
        KitBox kitBox = new KitBox();
        kitBox.setRoomId(roomId);
        kitBox.setRoomName(name);
        LambdaQueryWrapper<KitBox> query = new LambdaQueryWrapper<>();
        query.eq(KitBox::getRoomId, roomId);
        kitBoxMapper.update(kitBox, query);
        return true;
    }

    @Override
    public boolean delete(Long spaceId, SpaceTypeEnum spaceType) {
        LambdaQueryWrapper<KitBox> wrapper = new LambdaQueryWrapper<>();
        switch (spaceType) {
            case ROOM:
                wrapper.eq(KitBox::getRoomId, spaceId);
                break;
            case DEVICE:
                wrapper.eq(KitBox::getDeviceId, spaceId);
                break;
            case PANEL:
                wrapper.eq(KitBox::getPanelId, spaceId);
                break;
            case BOX:
                wrapper.eq(KitBox::getId, spaceId);
                break;
        }
        KitBox box = new KitBox();
        box.setIsDelete(1);
        return kitBoxMapper.update(box, wrapper) > 0;
    }

    @Override
    public boolean deleteByPanelIdList(List<Long> idList) {
        LambdaQueryWrapper<KitBox> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(KitBox::getPanelId, idList);
        KitBox box = new KitBox();
        box.setIsDelete(1);
        return kitBoxMapper.update(box, wrapper) > 0;
    }

    @Override
    public boolean deleteListById(List<Long> kitBoxIdList) {
        LambdaQueryWrapper<KitBox> query = new LambdaQueryWrapper<>();
        query.in(KitBox::getId,kitBoxIdList);
        KitBox kitBox = new KitBox();
        kitBox.setIsDelete(IsDeleteEnum.YES.getValue());
        return kitBoxMapper.update(kitBox,query)>0;
    }
}
